from main import db
from sqlalchemy import Column, String, Integer,Boolean, Float, ForeignKey ,DateTime
from sqlalchemy.orm import relationship
from flask_sqlalchemy import SQLAlchemy
import json
from datetime import datetime
from main.utils import TimeHelper
from flask import Flask
import config


class User(db.Model):
    '''用户'''
    __tablename__ = "user"
    id = Column(Integer(), primary_key=True)
    openid = Column(String(255))
    time = Column(db.DateTime, default=datetime.now())

    def to_json(self):
        dict = self.__dict__
        if "_sa_instance_state" in dict:
            del dict["_sa_instance_state"]
        return dict


class Questionnaire(db.Model):
    '''问卷'''
    __tablename__ = "questionnaire"
    id = Column(Integer(), primary_key=True)
    question = Column(String(255))
    type = Column(String(15))
    tips = Column(String(255))
    time = Column(db.DateTime, default=datetime.now())

    def to_json(self):
        dict = self.__dict__
        if "_sa_instance_state" in dict:
            del dict["_sa_instance_state"]
        return dict


class Option(db.Model):
    '''选项'''
    __tablename__ = "option"
    id = Column(Integer(), primary_key=True)
    question_id = Column(Integer, ForeignKey('questionnaire.id'))
    answer = Column(String(255))
    time = Column(db.DateTime, default=datetime.now())
    questionnaire_commit = relationship("QuestionnaireCommit", backref="option")

    def to_json(self):
        dict = self.__dict__
        if "_sa_instance_state" in dict:
            del dict["_sa_instance_state"]
        return dict


class SecondOption(db.Model):
    '''选项'''
    __tablename__ = "second_option"
    id = Column(Integer(), primary_key=True)
    option_id = Column(Integer, ForeignKey('option.id'))
    answer = Column(String(255))
    time = Column(db.DateTime, default=datetime.now())


    def to_json(self):
        dict = self.__dict__
        if "_sa_instance_state" in dict:
            del dict["_sa_instance_state"]
        return dict


class QuestionnaireCommit(db.Model):
    '''用户提交表单'''
    __tablename__ = "questionnaire_commit"
    id = Column(Integer(), primary_key=True)
    questionnaire_id = Column(Integer, ForeignKey('questionnaire.id'))
    option_id = Column(Integer, ForeignKey('option.id'))
    second_option_id = Column(Integer, ForeignKey('second_option.id'))
    user_id = Column(Integer, ForeignKey('user.id'))
    content = Column(String(255))
    time = Column(db.DateTime, default=datetime.now())

    def to_json(self):
        dict = self.__dict__
        if self.option:
            dict['option'] = self.option.answer
        if "_sa_instance_state" in dict:
            del dict["_sa_instance_state"]
        return dict


class Result(db.Model):
    '''游戏结果'''
    __tablename__ = "result"
    id = Column(Integer(), primary_key=True)
    user_id = Column(Integer, ForeignKey('user.id'))
    # 成本
    cost = Column(Float)
    # 售价
    price = Column(Float)
    # 回购价格
    repurchase = Column(Float)
    # 均值
    mean = Column(Float)
    # 方差
    variance = Column(Float)
    # 需求/天数(,分割)
    need = Column(String(255))
    day = Column(String(255))
    buy = Column(Integer)
    profit = Column(Float)
    game = Column(Integer)
    time = Column(db.DateTime, default=datetime.now())

    def to_json(self):
        dict = self.__dict__
        if "_sa_instance_state" in dict:
            del dict["_sa_instance_state"]
        return dict



